package com.jkhan.demo.date_api;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Demo03 {

    public static void main(String[] args) throws ParseException {

        //设置生日
        String birthday = "2000-05-14";

        //将String类型转换为日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(birthday);

        //对日期进行计算
        Date spec = prepareBirthday(date);

        //将计算后的结果，转换为String类型，输出
        String day = sdf.format(spec);
        System.out.println(birthday + "提前两周的周六为： " + day);

    }

    /**
     * 传入日期，计算出该日期的两个星期前的周六的日期
     *
     * @param birthday
     * @return
     */
    public static Date prepareBirthday(Date birthday) {

        Calendar calendar = new GregorianCalendar();
        calendar.setTime(birthday);
        //提前两周
        calendar.add(Calendar.WEEK_OF_MONTH, -2);
        //调整为当前星期的周六
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);

        return calendar.getTime();
    }
}
